package com.laze.mywanandroid.ui.main.fragment.project

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.loadmore.LoadMoreStatus
import com.google.android.flexbox.FlexboxLayoutManager
import com.laze.mywanandroid.R
import com.laze.mywanandroid.base.BaseVmFragment
import com.laze.mywanandroid.bean.ArticleData
import com.laze.mywanandroid.ui.details.DetailsActivity
import com.laze.mywanandroid.utils.core.ActivityUtil
import kotlinx.android.synthetic.main.project_fragment.*


class ProjectFragment : BaseVmFragment<ProjectViewModel>() {

    private val treeAdapter by lazy { ProjectTreeAdapter(R.layout.item_project_tree) }
    private val projectAdapter by lazy { ProjectListAdapter(R.layout.item_project) }

    companion object {
        fun newInstance() = ProjectFragment()
    }

    override fun layoutRes(): Int = R.layout.project_fragment

    override fun viewModelClass(): Class<ProjectViewModel> = ProjectViewModel::class.java

    override fun initView() {
        super.initView()
        liProjectSelect.setOnClickListener {
            viewModel.changeTree()
        }
        initRecyclerView()
    }

    override fun initData() {
        super.initData()
        viewModel.initProjectTree()
    }

    private fun initRecyclerView() {
        rvProjectTree.layoutManager = FlexboxLayoutManager(context)
        rvProjectTree.adapter = treeAdapter.apply {
            setOnItemClickListener { _, _, position ->
                getItem(position).let {
                    viewModel.changeProjectId(it.id)
                    tvProjectSelect.text = it.name
                    animateClose(llProjectTab)
                }
            }
        }
        rvProjectList.layoutManager = LinearLayoutManager(context)
        rvProjectList.adapter = projectAdapter.apply {
            setOnItemClickListener { _, _, position ->
                getItem(position).let {
                    ActivityUtil.start(
                        DetailsActivity::class.java,
                        mapOf(
                            DetailsActivity.PARAM_ARTICLE to ArticleData(
                                title = it.title,
                                link = it.link
                            )
                        )
                    )
                }
            }
            loadMoreModule.setOnLoadMoreListener {
                viewModel.loadMoreProject()
            }
        }
    }

    override fun observe() {
        super.observe()
        viewModel.projectTree.observe(viewLifecycleOwner, Observer {
            treeAdapter.setList(it)
            tvProjectSelect.text = "点击展开"
        })
        viewModel.projectList.observe(viewLifecycleOwner, Observer {
            projectAdapter.setList(it.datas)
        })
        viewModel.isOpen.observe(viewLifecycleOwner, Observer {
            if (it) animateOpen(llProjectTab)
            else animateClose(llProjectTab)
        })
        viewModel.loadMoreStatus.observe(viewLifecycleOwner, Observer {
            when (it) {
                LoadMoreStatus.Loading -> projectAdapter.loadMoreModule.loadMoreToLoading()
                LoadMoreStatus.Complete -> projectAdapter.loadMoreModule.loadMoreComplete()
                LoadMoreStatus.Fail -> projectAdapter.loadMoreModule.loadMoreFail()
                LoadMoreStatus.End -> projectAdapter.loadMoreModule.loadMoreEnd()
                else -> return@Observer
            }
        })
    }

    /**
     * 展开选项框
     */
    private fun animateOpen(v: View) {
        if (v.visibility == View.GONE) {
            val animator = createDropAnimator(v, 0, rvProjectTree.computeVerticalScrollRange())
            animator.addListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator?) {
                    imageProjectSelect.setImageResource(R.drawable.ic_baseline_expand_less)
                }
            })
            animator.apply {
                duration = 2000
                start()
                v.visibility = View.VISIBLE
            }
        }
    }

    /**
     * 关闭选项框
     */
    private fun animateClose(view: View) {
        val origHeight = view.height
        val animator = createDropAnimator(view, origHeight, 0)
        animator.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator?) {
                view.visibility = View.GONE
                imageProjectSelect.setImageResource(R.drawable.ic_baseline_expand_more)
            }
        })
        animator.apply {
            duration = 500
            start()
        }
    }

    private fun createDropAnimator(v: View, start: Int, end: Int): ValueAnimator {
        val animator = ValueAnimator.ofInt(start, end)
        animator.addUpdateListener { arg0 ->
            val value = arg0.animatedValue as Int
            val layoutParams = v.layoutParams
            layoutParams.height = value
            v.layoutParams = layoutParams
        }
        return animator
    }


}